<template>
	<view class="content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view :prop="option" :id="eChartId" class="echarts"></view>
		<!--    <button @click="changeOption">:change:prop="klinecharts.updateEcharts"更新数据</button>-->
		<!-- #endif -->
		<!-- #ifndef APP-PLUS || H5 -->
		<view>非 APP、H5 环境不支持</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		props: {
			eChartId: {
				type: String,
				require: false,
				default: ""
			}
			// option: {
			//   type: Object,
			//   require: true,
			//   default: {
			//     id: null
			//   }
			// },
			// defaultClickNumber: {
			//   type: Number,
			//   require: false,
			//   default: 0
			// }
		},
		data() {
			return {
				option: {},
				timer: null
			}
		},
		onLoad() {

		},
		methods: {
			changeChart(option, type) {
				let that = this
				if(that.timer){
					clearInterval(that.timer)
					that.timer = null
				}
				that.timer = setInterval(function() {
					if (myChart) {
						myChart.clear()
					}
					if (!echarts) {
						return
					}
					let myChart = echarts.init(document.getElementById(that.eChartId))
					if (myChart) {
						myChart.clear()
					}
					myChart.setOption(option)
					if(that.timer){
						clearInterval(that.timer)
						that.timer = null
					}
				}, 500)
				// do{
				//  let myChart = echarts.init(document.getElementById(this.eChartId))
				//  if(myChart){
				//    myChart.clear()
				//  }
				//  myChart.setOption(option)
				// }while(echarts)

				//this.option = option
			},
			// changeOption() {
			//   const data = this.option.series[0].data
			//   // 随机更新示例数据
			//   data.forEach((item, index) => {
			//     data.splice(index, 1, Math.random() * 40)
			//   })
			// },
			onViewClick(options) {

			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				//myChart = echarts.init(document.getElementsByClassName(this.eChartId)[0])
				// 观测更新的数据在 view 层可以直接访问到
				//myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				if (!myChart && echarts) {
					myChart = echarts.init(document.getElementById(this.eChartId))
					myChart.setOption(newValue)
				}
				// 监听 service 层数据变更
				
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 300px;
	}
</style>
